{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "95a3695a",
   "metadata": {},
   "source": [
    "## 1. APP 币本位合约交易-价格与数量函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a3715ee",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|round_quantity|下单数量圆整|\n",
    "|round_price|价格圆整|\n",
    "|get_quantity|根据产品的开仓金额、杠杆倍数、开仓价格获取购买数量|\n",
    "|quantity_to_f|将下单数量转化为字符串|\n",
    "|price_to_f|将价格转化为字符串|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bdaec41",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b42b3704",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceCM\n",
    "from binance_interface.app.utils import eprint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a33a6abc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceCM = BinanceCM(\n",
    "    key=key, secret=secret,\n",
    "    proxy_host=proxy_host\n",
    ")\n",
    "trade = binanceCM.trade"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eabade13",
   "metadata": {},
   "source": [
    "## 3. 下单数量圆整 round_quantity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6198d2c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': 10, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 以产品数量的最小精度向下取圆整\n",
    "quantity_result = trade.round_quantity(\n",
    "    symbol='BTCUSD_PERP',\n",
    "    quantity=10,\n",
    ")\n",
    "print(quantity_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "533deceb",
   "metadata": {},
   "source": [
    "## 4. 价格圆整 round_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "22e1fbd5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': 12345.7, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 以产品价格的最小精度取圆整\n",
    "price_result = trade.round_price(\n",
    "    price=12345.67891234,\n",
    "    symbol='BTCUSD_PERP',\n",
    "    type='CEIL',  # 向上取圆整\n",
    ")\n",
    "print(price_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b0393f23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': 12345.6, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# 以产品价格的最小精度取圆整\n",
    "price_result = trade.round_price(\n",
    "    price=12345.67891234,\n",
    "    symbol='BTCUSD_PERP',\n",
    "    type='FLOOR',  # 向下取圆整\n",
    ")\n",
    "print(price_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00de8ef4",
   "metadata": {},
   "source": [
    "## 5. 根据产品的开仓金额、杠杆倍数、开仓价格获取购买数量 get_quantity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8984b99b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': 212, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "# openMoney为货币数量\n",
    "quantity_result = trade.get_quantity(\n",
    "    openPrice=21212.12,\n",
    "    openMoney=1,\n",
    "    symbol='BTCUSD_PERP',\n",
    "    leverage=1,\n",
    ")\n",
    "print(quantity_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86ed33f3",
   "metadata": {},
   "source": [
    "## 6. 将下单数量转化为字符串 quantity_to_f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d705dd47",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': '10', 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "quantity_to_f_result = trade.quantity_to_f(\n",
    "    quantity=10,\n",
    "    symbol='BTCUSD_PERP',\n",
    ")\n",
    "print(quantity_to_f_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c59fe93",
   "metadata": {},
   "source": [
    "## 7. 将价格转化为字符串 price_to_f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b3876470",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': '12345.1', 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "price_to_f_result = trade.price_to_f(\n",
    "    price=12345.1,\n",
    "    symbol='BTCUSD_PERP',\n",
    ")\n",
    "print(price_to_f_result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
